/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = ut__frstat_server_output.c                         */
/*  NOTE      =                                                    */
/*  DATE      = 2015/01/30 by guojq                                */
/*******************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <CUnit/Basic.h>
#include <CUnit/CUnit.h>

#include "frcomm.h"
#include "frmem.h"
#include "frlist.h"
#include "frhash2.h"
#include "frstat_server.h"
#include "apmtest_cunit.h"

extern int g_i_flag_run;
extern unsigned long g_ul_pcap_queue_used;

typedef struct __conn_info {
    unsigned long ul_connections;
    unsigned long ul_requests;
    unsigned long ul_responses;
} conn_info;

typedef struct __conn_avg {
    unsigned long ul_connections[4];
    unsigned long ul_requests[4];
    unsigned long ul_responses[4];
} conn_avg;

typedef struct __server_info {
    u_int   ui_ip;
    u_short us_port;
    int     i_l7_proto;
    
    struct timeval start_time;
    conn_info st_conns_total;
    conn_avg  st_conns_avg;
} Server_info;

//端口关联表
typedef struct __port_related {
    u_int   ui_ip;
    u_short us_port;
    u_int   ui_ip_related;
    u_short us_port_related;
    
    struct timeval start_time;
} Port_related;

//做完关联后的hash节点
typedef struct __server_info_avg {
    u_int   ui_ip;
    u_short us_port;
    int     i_l7_proto;
    
    unsigned long ul_connections;
    unsigned long ul_requests;
    unsigned long ul_responses;
    
    unsigned long ul_connections_avg;
    unsigned long ul_requests_avg;
    unsigned long ul_responses_avg;
} Server_info_avg;

extern Frhash_header g_st_hash_server;

static int __suite_init(void)
{
    g_i_flag_run = 1;
    g_ul_pcap_queue_used = 0L;
	return 0;
}

static int __suite_cleanup(void)
{
	return 0;
}

typedef struct __server_result {
    void *accept_sock;
    int  i_do_zmq;
} Server_result;


extern void __outhash_node_free(Frlist_node* p_node);
extern int __frstat_server_output(Frlist_node* p_node, const void *p_data);

static void case1(void)
{
    Frlist_node *p_st_node = NULL;
    Server_info_avg *p_server_info = NULL;
    Server_result st_out;
    int i_ret = 0;

    memset(&st_out, 0x00, sizeof(Server_result));

    p_server_info = (Server_info_avg *)frmalloc(sizeof(Server_info_avg));
    memset(p_server_info, 0x00, sizeof(Server_info_avg));
    
    p_server_info->ui_ip = 16781484;
    p_server_info->us_port = 80;
    p_server_info->i_l7_proto = 1433;
    
    p_server_info->ul_connections = 300;
    p_server_info->ul_requests = 200;
    p_server_info->ul_responses = 100;
    p_server_info->ul_connections_avg = 50;
    p_server_info->ul_requests_avg = 30;
    p_server_info->ul_responses_avg = 10;
    
    p_st_node = frlist_node_create(p_server_info);
    
    i_ret = __frstat_server_output(p_st_node, &st_out);

//    CU_ASSERT_STRING_EQUAL(arr_out,"ip=172.16.0.1, port=80, l7_proto=1433, connections=300(50/s), requests=200(30/s), responses=100(10/s)\n");
    CU_ASSERT_EQUAL(i_ret, 1);

    __outhash_node_free(p_st_node);
    
    frfree(p_st_node);
    
    return;

}

int add_suite()
{
	CU_pSuite pSuite = NULL;

	pSuite = CU_add_suite("ut__frstat_server_output 测试", __suite_init, __suite_cleanup);

	if (NULL == pSuite)
	{
		return 1;
	}


    //增加测试用例
   	CU_add_test(pSuite, "case1", case1);

    return 0;

}

int main(int argc, char **argv)
{
    fr_slab_init(52428800, "test");
    run_cunit(add_suite);
    fr_slab_destroy();
    return 0;
}